package javaBase.LinkList;

/**
 * @program: jvm
 * @description
 * @author: tkk fendoukaoziji
 * @create: 2019-05-01 10:07
 **/
public class MyLinkList {
    private Link first;

    public MyLinkList() {
        first = null;
    }
    public  boolean isEmpty(){
        return (first==null);
    }

    public void insertFirst(int id,double dd ){
        Link newLink = new Link(id, dd);
        Link next = newLink.getNext();
        next=first;
        newLink.setNext(next);//newLink--> old first   &#x6307;&#x5411;&#x8001;&#x7684;first&#x5730;&#x5740;
        first=newLink;//first-->newLink      将first引用指向新link
    }



    public Link deleteFirst(){
        Link temp=first;  //保存第一个元素本来的引用地址
        first=first.getNext();  //delete first  first-->old first
        return temp;
    }

    public void displayList(){
        System.out.print("List (first -->last):");
        Link current=first;// start at beginning of list
        while (current!=null){
            current.displayLink();
            current=current.getNext();
        }
        System.out.println("");
    }

    public Link find(int key){
        Link curr =first;            //从第一个开始，
        while(curr.getiData()!=key){  //while no match
           if(curr.getNext()==null){ // if end of list
               return null;        //didn't find it
           }else {
               curr=curr.getNext(); //not end of list   go to next link
           }
        }
        return  curr;      //found it
    }

    public Link delete(int key){
        Link curr=first;
        Link previous=first;
        while (curr.getiData()!=key){
            if(curr.getNext()!=null){
                curr=curr.getNext();
                previous=curr;
            }else{
                return  null;
            }
        }
        if(curr==first){ // 如果删除的是首节点，改变首节点
            first=first.getNext();
        }else{
            previous=curr.getNext(); //否则，略过节点
        }
        return  curr;
    }
}
















